package com.ceba.ep.test;

import com.ceba.cb.bean.CPDFRectangleParam;
import com.ceba.cb.bean.CPDFSignParam;
import com.ceba.cebacer.EncodeSignType;
import com.ceba.cebacer.EnterpriseType;
import com.ceba.cebacer.HashType;
import com.ceba.cebacer.KeyLoaderType;
import com.ceba.cebautils.IDUtils;
import com.ceba.cpdf.bean.PDFParam;
import com.ceba.cpdf.server.ks.KSPackSignServiceParam;
import com.ceba.cpdf.server.ta.TAPackSignServiceParam;
import com.ceba.cpdf.strategy.CPDFUtils;
import com.ceba.cpdf.strategy.bean.CPDFStrategyX;
import com.ceba.cpdf.strategy.bean.CQPDFStrategy;
import com.ceba.cpdf.strategy.context.CPDFContext;
import com.ceba.cpdf.tool.CbDSVSClient;
import com.ceba.cq.bean.params.CQPDFFrontRectangle;
import com.ceba.cq.bean.params.CQPDFFrontSignInfo;
import com.ceba.cq.bean.params.CQPDFFrontSignParam;
import com.ceba.service.bean.CMessage;
import com.itextpdf.text.pdf.PdfSignatureAppearance;
import org.json.JSONObject;
import org.junit.Test;


public class PdfSignTest0724 {

	static String imagePath = "E:/test/images/stamp/sign.png";
	/*private static String ip = "106.37.221.203";// 服务ip "106.37.221.203" 192.168.0.188
												// "116.226.122.181"
	private static int port = 8288;// 服务端口
*/
	//7d81514cffe7fd47  41e0e72ab3468357
	private String serviceUrl = "http://114.113.114.175:8080/SVS/dsvs";
	//5543b03e8c20d4ca   3c2eb0c71efe937f
//	private static String serviceUrl = "http://192.168.0.88/dsvs";
//	private static String serviceUrl = "http://raadmin.cqcca.com:8080/SVS/dsvs";
//	private static String tsaUrl="http://114.113.114.175:8080/tsa/tsa";
//	private static String tsaUrl ="http://106.38.71.230:8080/tsa/tsa";
	private static String tsaUrl ="http://timestamp.wosign.com/timestamp";
//	private static String tsaUrl="http://192.168.5.86:8080/tsa/tsa";
	private static String src = "E:/test/1/t/3331.pdf";
	static String rsaDest = "E:/test/1/t/rsa.pdf";
	static String sm2Dest = "E:/test/1/t/3331.pdf";
	static String otherDest = "E:/test/1/t/y4.pdf";
	static CPDFContext cqpdfContext = new CPDFContext(new CQPDFStrategy());

	static CPDFContext cpdfContext = new CPDFContext(new CPDFStrategyX());

	public static void main(String[] args) {

	}

	public static String certInfo ="MIIFcjCCBNugAwIBAgIQdR3oawK1Fu0iF5iDafzopTANBgkqhkiG9w0BAQUF"
			+ "ADAgMQ4wDAYDVQQKEwVTSEVDQTEOMAwGA1UEAxMFU0hFQ0EwHhcNMTYwMzMwMTYwMDAwWhcNMTkwMzMwM"
			+ "TYwMDAwWjCBlDELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAlNIMQswCQYDVQQHEwJTSDENMAsGA1UEChMEU0"
			+ "hDQjEtMCsGA1UECwwk5LiK5rW35rWL5ZCn5L+h5oGv5oqA5pyv5pyJ6ZmQ5YWs5Y+4MS0wKwYDVQQDDCT"
			+ "kuIrmtbfmtYvlkKfkv6Hmga/mioDmnK/mnInpmZDlhazlj7gwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw"
			+ "ggEKAoIBAQDxdZkV0ThyO7UymSfAabbbi64uUMXpUj156cRw25hbGMQYgV/Vkiv0+xcO+jdWYs1B9a4BR"
			+ "Lr/O8sD4dYzdpmx9pnKrwUQietGslwjdG2q3rAQkRYKOFDSR6g29dZCLb1z4dia+voWiLk9yBCtk8z9H8"
			+ "MIecULxHy8pDNPBlfqa1I67MYdkhYbCbN3C17KyD+RvFMPqyC1C8Sah72q3vLHAK4+8yB8pK6BFOHPjBG"
			+ "N+TCEmUJDryYHlmdV7mrn5ynjS5/J/nssWoq+M3FUH2zEO/ZRReugk1nOmrga/UL9MJEadQhVRXFG7clb"
			+ "QcESqBO2nmVhgDcWrArVyupHub/JAgMBAAGjggKyMIICrjAfBgNVHSMEGDAWgBTUUMl0k300mra/yCIEU"
			+ "Nh8RTyFcDAdBgNVHQ4EFgQUgh85lF0yLJbxBa5bg9Q3eWF91x4wCwYDVR0PBAQDAgbAMB0GA1UdJQQWMB"
			+ "QGCCsG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SD1WrYiiB";
	
	@Test
	public void testCerInfo(){
		CbDSVSClient client = new CbDSVSClient(serviceUrl);
		client.getCertInfo(certInfo);
	}

	@Test
	public void testKEYRSA() {
		try {

			CPDFSignParam siParam = new CPDFSignParam();

			siParam.setKeyWord("申請人");
			siParam.setImgPath(imagePath);
			siParam.setCertificationLevel(2);
			siParam.setSignInfo(siParam.getDefaultSignInfo());
			siParam.setImgH(75);
			siParam.setImgW(75);
			
			PDFParam param = new PDFParam(KeyLoaderType.TYPE_CEBA_KEY, siParam);

			CPDFRectangleParam rect = new CPDFRectangleParam();
			rect.setImgPath(imagePath);
			rect.setImgH(75);
			rect.setImgW(75);
			rect.setImgX(433);
			rect.setImgY(100);
			rect.setPageNo(1);
			rect.setShowPic(true);
			
			param.setcRectangleParm(rect);
			param.setNeedRect(true);
			param.setNeedLtv(true);
			src = src;
			if(CPDFUtils.checkFileHasRSASigned(src)){
				siParam.setPdfHasRSASigned(true);
			}
			if(CPDFUtils.checkFileHasSM2Signed(src)){
				siParam.setPdfHasSM2Signed(true);
			}
			CMessage cm = CPDFUtils.pdfSignByRectangle(param, src, otherDest);
			
			System.out.println(new JSONObject(cm).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testCerRSA() {
		try {

			CPDFSignParam cpdfSignParam = new CPDFSignParam();
			cpdfSignParam.setKeyWord("");
			cpdfSignParam.setImgPath(imagePath);
			cpdfSignParam.setImgH(75);
			cpdfSignParam.setImgW(75);
			cpdfSignParam.setShowPic(true);
            cpdfSignParam.setCertificationLevel(PdfSignatureAppearance.NOT_CERTIFIED);

			//位置定位参数
			CPDFRectangleParam rect = new CPDFRectangleParam();
			rect.setSignInfo(cpdfSignParam.getDefaultSignInfo());
			rect.setImgPath(imagePath);
			rect.setImgH(75);
			rect.setImgW(75);
			rect.setImgX(433);
			rect.setImgY(238);
			rect.setPageNo(1);
			rect.setShowPic(true);
			
			PDFParam param = new PDFParam();
			param.setcSignParam(cpdfSignParam);
			param.setcRectangleParm(rect);
			param.setKeyType(KeyLoaderType.TYPE_P12);
			param.setNeedRect(true);
			param.setNeedLtv(true);
			param.setcRectangleParm(rect);
			src = rsaDest;
			if(CPDFUtils.checkFileHasRSASigned(src)){
				cpdfSignParam.setPdfHasRSASigned(true);
			}
			if(CPDFUtils.checkFileHasSM2Signed(src)){
				cpdfSignParam.setPdfHasSM2Signed(true);
			}
			CMessage cm = CPDFUtils.pdfSignByKeyWord(param, src, otherDest);
		
			System.out.println(new JSONObject(cm).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testKSRSA() {
		try {

			CQPDFFrontSignParam siParam = new CQPDFFrontSignParam();
			siParam.setServiceIp("103.36.136.173");
			siParam.setServicePort(7845);
			siParam.setServerUrlSuffix("/services/CipherServices");
			siParam.setCerNo("Cont2");
			siParam.setPrintSeal(false);
			siParam.setFieldName("signature1");
			//siParam.setFatherCerType(EnterpriseType.TYPE_SHACA_ROOT_RSA.getCode());
			siParam.setFatherCerType(EnterpriseType.TYPE_SHACA_ROOT_G2_RSA.getCode());
			siParam.setNeedCompleteCer(true);
			CQPDFFrontSignInfo signInfo = new CQPDFFrontSignInfo("实验室报告", "上海",3);
			siParam.setSignInfo(signInfo);
			siParam.setImgPath(imagePath);
			siParam.setImgW(100);
			siParam.setImgH(100);
			//位置定位参数
			CQPDFFrontRectangle rect = new CQPDFFrontRectangle();
			rect.setPageNo(1);
			rect.setTop(200);
			rect.setRight(200);
			rect.setLeft(0);
			rect.setBottom(0);
			siParam.setRectangle(rect);
			
			PDFParam param = new PDFParam();
			param.setCqSignParm(siParam);
			param.setNeedRect(true);
			param.setKeyType(KeyLoaderType.TYPE_SERVER_KS);
			param.setEncodeSignType(EncodeSignType.RSA.getCode());
			if(CPDFUtils.checkFileHasRSASigned(src)){
				siParam.setPdfHasRSASigned(true);
			}
			if(CPDFUtils.checkFileHasSM2Signed(src)){
				siParam.setPdfHasSM2Signed(true);
			}
			JSONObject requestObject = new JSONObject();
			requestObject.put("appCode", "CBTS");
			requestObject.put("appPWD", "12345678");
			param.setHashType(HashType.SHA2.getType());
			KSPackSignServiceParam.packKSServiceParam(param,requestObject);
			CMessage cm = cqpdfContext.pdfSignByRectangle(param, src, rsaDest);
			
			System.out.println(new JSONObject(cm).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Test
	public void testTARSA() {
		try {

			CQPDFFrontSignParam siParam = new CQPDFFrontSignParam();
			siParam.setServiceIp("122.14.61.13");
			siParam.setCerNo("Test1");
			siParam.setPrivateKeyName("Test1");
			siParam.setPrintSeal(false);
			siParam.setFieldName("signature");
			//siParam.setFatherCerType(EnterpriseType.TYPE_SHACA_ROOT_RSA.getCode());
//			siParam.setFatherCerType(EnterpriseType.TYPE_SHACA_ROOT_G2_RSA.getCode());
			siParam.setNeedCompleteCer(false);
			CQPDFFrontSignInfo signInfo = new CQPDFFrontSignInfo("实验室报告", "上海",3);
			siParam.setSignInfo(signInfo);
			siParam.setImgPath(imagePath);
			siParam.setImgW(100);
			siParam.setImgH(100);
			//位置定位参数
			CQPDFFrontRectangle rect = new CQPDFFrontRectangle();
			rect.setPageNo(1);
			rect.setTop(200);
			rect.setRight(200);
			rect.setLeft(0);
			rect.setBottom(0);
			siParam.setRectangle(rect);

			PDFParam param = new PDFParam();
			param.setCqSignParm(siParam);
			param.setNeedRect(true);
			param.setKeyType(KeyLoaderType.TYPE_SERVER_TA);
			param.setEncodeSignType(EncodeSignType.RSA.getCode());
			if(CPDFUtils.checkFileHasRSASigned(src)){
				siParam.setPdfHasRSASigned(true);
			}
			if(CPDFUtils.checkFileHasSM2Signed(src)){
				siParam.setPdfHasSM2Signed(true);
			}
			JSONObject requestObject = new JSONObject();
			param.setHashType(HashType.SHA1.getType());
            TAPackSignServiceParam.packTAServiceParam(param,requestObject);
			CMessage cm = cqpdfContext.pdfSignByRectangle(param, src, rsaDest);

			System.out.println(new JSONObject(cm).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
